<?php
class guestbookPlugin extends commonMod
{
    public function __construct()
    {
        parent::__construct();        
        $this->tpl = new cpTemplate($this->config);
    }
    //配置读取
    public function plus_configout()
    {       
        return unserialize($this->plus_setting(dirname(__file__),'config'));

    }
//---------------------------------------全局信息结束----------------------------------------------------//

    //留言首页
    public function index()
    {
         	$config=$this->plus_configout();
        $listRows = $config['page']; //每页显示的信息条数
        $count = $this->model->table('extend_guestbook')->where($condition)->count(); //获取行数
		
		$url = __URL__ . '/index/page-{page}.html'; //分页基准网址
        $page = new Page();
        $cur_page = $page->getCurPage($url);
        $limit_start = ($cur_page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;
		//附加程序start
		$datestamp = isset($_GET['date']) ? strtotime($_GET['date']) : time();
		$week = date("w", $datestamp);
		$week = $week == 0 ? 7 : $week;
		$hour = date("H");
		$day = date("Yz", $datestamp) - date("Yz");
		$add = '';
		if($day == 0) {
			$add = '（今天）';
		} elseif($day == 1) {
			$add = '（明天）';
		} elseif($day == 2) {
			$add = '（后天）';
		}
		$this->assign('add', $add);
		$this->assign('currentday', date("Y年m月d号", $datestamp));
		$str = '';
		if($hour < 18 || $day != 0) {
			$str .= " AND (outpatient_time LIKE '%,{$week}3%' OR special_outpatient_time LIKE '%,{$week}3%'";
		} else {
			$str .= " AND (cid<0";
		}
		if($hour < 14 || $day != 0) {
			$str .= " OR outpatient_time LIKE '%,{$week}2%' OR special_outpatient_time LIKE '%,{$week}2%'";
		}
		if($hour < 8 ||  $day != 0) {
			$str .= " OR outpatient_time LIKE '%,{$week}1%' OR special_outpatient_time LIKE '%,{$week}1%'";
		}
		$str .= ')';
		//echo $str;
       // $sql = "SELECT A.*,B.pid,B.type as ctype,B.name as cname,B.seoname as cseoname,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid=B.id WHERE cid=30 $str ORDER BY A.sorter DESC LIMIT {$limit}";
        $loop = $this->model->query($sql); //执行查询
		//附加程序end
        //$loop = $this->model->table('extend_guestbook')->where('status=1')->order('id desc')->limit($limit)->select();
        $this->assign('loop', $loop);
        $this->assign('ipdis',$config['ipdis']);
        $this->assign('page', $this->page($url, $count, $listRows));
        $this->display('guestbook');
    }
    //添加
    public function add()
    {
        $config=$this->plus_configout();
        $data = array();
        $data['name'] = $this->filter(in($_POST['name']));
        $data['tel'] = $this->filter(in($_POST['tel']));
        $data['qq'] = $this->filter(in($_POST['qq']));
        $data['ip'] = get_client_ip();
        $data['time'] = time(); //时间
        $data['content'] = $this->filter(in($_POST['content']));
        if (empty($data['name']))
        {
            $this->error('称呼不能为空');
        }
        if (empty($data['content']))
        {
            $this->error('内容不能为空');
        }
/*         if ($_POST['checkcode'] != $_SESSION['verify']) {
            $this->error('验证码错误');
            return;
        } */

        //数据库操作       
        $this->model->table('extend_guestbook')->data($data)->insert();

        echo "true";

    }

    //内容过滤
    public function filter($content)
    {
        
        $config=$this->plus_configout();
        $arry=explode("|",$config['filter']);
        foreach ($arry as $value) {
          $content=str_replace($value, '****', $content);
        }
        return $content;
    }

	
	//留言列表栏
    public function lists()
    {
      	$list = $this->model->table('extend_guestbook')->where('status=1')->order('id desc')->limit(5)->select();
		$this->assign('list', $list);
        $this->display('guestbook_list');
    }

    // 生成验证码
    public function verify()
    {
        Image::buildImageVerify();
    }


}

?>